package zj.com.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ListView;

import com.zfdang.multiple_images_selector.SelectorSettings;

import java.util.ArrayList;
import java.util.List;

import zj.com.activities.DqrJcxDetailActivity;
import zj.com.adapter.DqrJcxAdapter;
import zj.com.adapter.UploadImgGridAdapter;
import zj.com.app.ZjApplication;
import zj.com.model.DjcModel;
import zj.com.zj.R;

/**
 * Created by xi on 16/6/4.
 * 待整改问题编辑
 */
public class DzgDetailEditFragment extends Fragment {


    public Intent intent;

    public GridView uploadGridView;
    public List<String> list;
    public UploadImgGridAdapter adapter;
    public static final int REQUEST_CODE = 732;

    public  static ArrayList<String> mResults = new ArrayList<>();
    public EditText des_et;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_dzg_detail_edit, container, false);
        initView(view);
        initData();
        return view;
    }


    public void initView(View view){

        des_et = (EditText) view.findViewById(R.id.dec_et);
        uploadGridView = (GridView) view.findViewById(R.id.uploadGridView);

        list = new ArrayList<>();
        list.add("");
        adapter = new UploadImgGridAdapter(list,getActivity());
        uploadGridView.setAdapter(adapter);

        if(ZjApplication.dzgInspectItemDB != null && ZjApplication.dzgInspectItemDB.getRepairDesc() != null){
            des_et.setText(ZjApplication.dzgInspectItemDB.getRepairDesc());

            list.clear();
            String img = ZjApplication.dzgInspectItemDB.getImgs();
            if(img != null){
             String[] imgs =    img.split(",");
                for(String s:imgs){
                    list.add(s);
                }
            }
            list.add("");
        }

        des_et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {


                String content = s.toString();
                if (ZjApplication.dzgInspectItemDB != null) {
                    ZjApplication.dzgInspectItemDB.setRepairDesc(content);
                }
            }
        });
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // get selected images from selector
        if(requestCode == REQUEST_CODE) {
            if(resultCode == getActivity().RESULT_OK) {
                mResults = data.getStringArrayListExtra(SelectorSettings.SELECTOR_RESULTS);
                assert mResults != null;

                // show results in textview
                StringBuilder sb = new StringBuilder();
                sb.append(String.format("Totally %d images selected:", mResults.size())).append("\n");
                list.clear();
                StringBuffer imgSB = new StringBuffer();
                for(String result : mResults) {
                    list.add(result);
                    sb.append(result).append("\n");
                    imgSB.append(result);
                    imgSB.append(",");
                }
                Log.e("wxf", sb.toString());

                if(ZjApplication.dzgInspectItemDB != null){
                    ZjApplication.dzgInspectItemDB.setImgs(imgSB.toString());
                };
                list.add("");
                adapter.setDataChange(list);
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }


    public void initData(){


    }
}
